const express = require('express')
const app = express()
const port = 3333

//引入路由文件
const router = require('./router.js')

const bodyParser = require('body-parser')

//开启静态文件
app.use('/www',express.static('public'));

app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json()) 



app.get('/', (req, res) => res.send('Hello World!-vue-shoppingMall'))

app.listen(port, () => {
	console.log(`running.......... on port ${port}!`)
})

// 解决跨域问题
// app.all('*', function(req, response, next) {
// 	//设置允许跨域的域名，*代表允许任意域名跨域
// 	response.header("Access-Control-Allow-Origin", "*");
// 	//允许的header类型
// 	response.header("Access-Control-Allow-Headers", "X-Requested-With");
// 	//跨域允许的请求方式
// 	response.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
// 	//设置响应头信息
// 	response.header("X-Powered-By",' 3.2.1')
// 	response.header("Content-Type", "application/json;charset=utf-8");

// 	next();

// });


// app.all('*', function (req, res, next) {
//   res.header('Access-Control-Allow-Origin', req.headers.origin);//获取请求源 这样所有请求就都有访问权限了
//   res.header('Access-Control-Allow-Credentials', true);
//   res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With')
//   res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
//   res.header('Content-Type', 'application/json;charset=utf-8');
//   next()
// });


app.all('*', function(req, res, next) {
            res.header("Access-Control-Allow-Origin", "*");
            res.header("Access-Control-Allow-Headers", "X-Requested-With");
            res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
            res.header("X-Powered-By",' 3.2.1')
            res.header("Content-Type", "application/json;charset=utf-8");
            next();
});
app.use(router)

